<template>
	<el-dialog v-model="isVisible" :title="row ? '编辑价值管理' : '新增价值管理'" width="600px" :before-close="handleClose">
		<el-form ref="formRef" class="worth-form" :model="form" :rules="rules" label-width="100px">
			<el-form-item label="物料名称" prop="materialName">
				<el-input v-model="form.materialName" placeholder="输入物料名称" :maxlength="30" />
			</el-form-item>
			<el-form-item label="物料编码" prop="materialCode">
				<el-input v-model="form.materialCode" placeholder="输入物料名称" :maxlength="30" />
			</el-form-item>
			<el-form-item label="生产年份" prop="productYear">
				<el-input-number :controls="false" class="num-left mr10" v-model="form.productYear" :min="0" :max="9999999999" placeholder="输入生产年份" />年
			</el-form-item>
			<el-form-item label="单件价值" prop="value">
				<el-input-number
					:controls="false"
					:precision="2"
					class="num-left"
					v-model="form.value"
					:min="0"
					:max="9999999999"
					placeholder="输入单件价值"
				/>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">取消</el-button>
				<el-button type="primary" v-debounce="submit">保存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { FormRules, ElMessage } from 'element-plus';
import { ref, reactive, watch, onMounted } from 'vue';
import { fetchCreateWorth, fetchWorthDetail, fetchUpdateWorth } from '/@/api/system';
import { fenToYuan } from '/@/utils/commonFunction';

const props = defineProps({
	// 列表内容
	visible: {
		type: Boolean,
		default: () => false,
	},
	row: {
		type: Object,
		default: () => null,
	},
});

const emit = defineEmits(['handleClose']);

const isVisible = ref(false);

const formRef = ref();
const form = ref<any>({});

//校验规则
const rules = reactive<FormRules<any>>({
	materialCode: [{ required: true, trigger: 'blur', message: '物料编码不能为空' }],
	materialName: [{ required: true, trigger: 'blur', message: '物料名称不能为空' }],
	productYear: [{ required: true, trigger: 'blur', message: '生产年份不能为空' }],
	value: [{ required: true, trigger: 'blur', message: '价值不能为空' }],
});

watch(
	() => props.visible,
	async (val) => {
		if (val && props.row) {
			if (props.row.id) {
				const res: any = await fetchWorthDetail({ id: props.row.id });
				if (res.code === 200) {
					form.value = {
						...res.data,
						value: fenToYuan(res.data.value),
					};
				}
			}
		}
		isVisible.value = val;
	},
	{
		immediate: true,
	}
);

const submit = async () => {
	formRef.value.validate(async (valid: boolean) => {
		if (valid) {
			const params = { ...form.value, value: form.value.value * 100 };
			const res = props.row ? await fetchUpdateWorth(params) : await fetchCreateWorth(params);
			if (res.code == 200) {
				ElMessage.success(props.row ? '编辑价值管理成功' : '新增价值管理成功');
				handleClose(true);
			} else {
				ElMessage.error(res.msg);
			}
		}
	});
};

const handleClose = (success?: boolean) => {
	formRef.value.resetFields();
	emit('handleClose', success === true);
};
</script>

<style lang="scss">
.worth-form {
	height: 250px;
	.el-form-item {
		width: 90%;
		display: flex;
		align-items: center;
		.el-input-number {
			flex-grow: 1;
		}
	}
}
</style>
